package com.utils;

import java.io.*;

public class FileUtils {

    public static void main(String[] args) throws IOException {
        System.out.println("Hello World!");

        String path = "D:\\demo.txt";
        String str = "我是content1234";
        //boolean status = writeContentToFile(path,str);
        //System.out.println(status);
        //String content = readContentFromFile(path);
        //System.out.println(content);
        //FileInputStream fis = new FileInputStream(new File(path));
        //BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
        //writeContentToFile(fis,"D:\\demo2.txt");
        writeToFileByBytes(readFromFilePathReStream("D:\\demo.txt"),"D://demo4.txt");
        System.out.println(new String(readFromFilePathReStream("D:\\demo.txt"))+"11111111111111");
    }

    public static byte[] readFromFilePathReStream(String path) throws IOException {
        if(path==null) return null;
        File file = new File(path);
        if(!file.exists()) return null;
        FileInputStream fis = new FileInputStream(file);
        byte[] bytes = new byte[(int)file.length()];
        fis.read(bytes);
        fis.close();
        return bytes;
    }

    public static String readContentFromFile(String path) throws IOException {
        if(path==null) return null;
        File file = new File(path);
        if(!file.exists()) return null;

        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
        StringBuilder stringBuilder = new StringBuilder();
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = bufferedInputStream.read(buffer))!=-1){
            stringBuilder.append(new String(buffer));
        }
        return stringBuilder.toString();
    }

    /**
     * 向文件中写入内容
     *
     * */
    public static  boolean writeContentToFile(String path,String content) throws IOException {
        boolean result = false;
        if(path==null){
            System.out.println("文件路径不能为null");
            return result;
        }
        File file = new File(path);
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        byte[] strbt = content.getBytes();
        fos.write(strbt,0,strbt.length);
        fos.close();
        return true;
    }

    public static void writeContentToFile(FileInputStream fis,String localPath) throws IOException {
        if(fis == null || localPath==null){
            System.out.print("参数信息错误");
            return ;
        }
        System.out.println("filepath:"+localPath);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
        File file = new File(localPath);
        if(!file.exists()) file.createNewFile();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(file));
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len=bufferedInputStream.read(buffer))!=-1){
            bufferedOutputStream.write(buffer,0,len);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
        System.out.println("completed!");
    }

    public static void writeToFileByBytes(byte[] bytes,String localPath) throws IOException {
        File file = new File(localPath);
        if(!file.exists()) file.createNewFile();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                new FileOutputStream(file));
        bufferedOutputStream.write(bytes,0,bytes.length);
        bufferedOutputStream.close();
    }



}
